package org.whuims.pois.action;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.whuims.pois.dao.SiteDao;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.SiteC;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhongwei on 2014/5/14.
 */
public class SiteAction extends DispatchAction {
    BaseDao baseDao = new BaseDao();
    SiteDao siteDao = new SiteDao();

    public ActionForward show(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {
        List<SiteC> list = new ArrayList<SiteC>();
        list = siteDao.select();
        request.setAttribute("result", list);
        return mapping.findForward("show");
    }

    public ActionForward insert(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        SiteC bean = (SiteC) form;
        String siteName = request.getParameter("siteName");

        if (siteName.trim().equals("")) {
            request.setAttribute("errormessage", "站点名不能为空");
            return mapping.findForward("error");
        }

        if (bean.getId() > 0) {//更新
            siteDao.modify(bean);
        } else {
            if (baseDao.getTotalCounts("from SiteC where siteName=?", siteName) > 0) {
                request.setAttribute("errormessage", "站点名已经存在");
                return mapping.findForward("error");
            }
            siteDao.insert(bean);
        }
        return mapping.findForward("success");
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form,
                              HttpServletRequest request, HttpServletResponse response) {

        String siteId = request.getParameter("siteId");
        Long id = Long.parseLong(siteId);
        SiteC bean = siteDao.select(id);
        request.setAttribute("bean", bean);
        return mapping.findForward("edit");
    }

    public ActionForward modify(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        SiteC bean = (SiteC) form;
        if (bean.getSiteName().trim().equals("")) {
            request.setAttribute("errormessage", "站点名不能为空");
            return mapping.findForward("error");
        }
        siteDao.modify(bean);
        return mapping.findForward("success");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        String siteId = request.getParameter("siteId");
        Long id = Long.parseLong(siteId);
        SiteC bean = siteDao.select(id);
        siteDao.delete(bean);
        return mapping.findForward("success");
    }
}
